/**
 * 用户相关服务
 */

const util = require('../utils/util.js');
const api = require('../config/api.js');


/**
 * 调用微信登录
 */
function loginByWeixin(e, dom) {
  var route = dom.route;
  var userInfo = e.detail.userInfo;
  if (typeof (userInfo) === "undefined") {
    return false;
  }
  wx.login({
    success: function (res) {
      if (res.code) {
        var ri = wx.getStorageSync("recommendId");
        if(!ri){
          ri = "";
        }
        var data = {
          code: res.code,
          gender: userInfo.gender,
          avatarUrl: userInfo.avatarUrl,
          nickName: userInfo.nickName,
          recommendId: ri
        };
        wx.request({
          //后台接口地址
          url: api.LoginUrl,
          data: data,
          method: 'POST',
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: function (res) {
            if (res.data.type == 'success') {
              wx.setStorageSync('token', res.data.data.token);
              wx.setStorageSync('openid', res.data.data.openid);
              wx.setStorageSync('memberId', res.data.data.memberId);
              console.log('已登录');
              dom.setData({
                hiddenName: true
              });
              if (route == 'pages/index3/index3') {
                dom.getMyselfData();
                dom.getIndexData();
              } else if (route == 'pages/catalog/catalog') {
                dom.getCatalog();
              } else if (route == 'pages/cart/cart') {
                dom.getCartList();
                dom.setData({
                  currentAmount: 0.00,
                  checkedAllStatus: false
                });
              } else if (route == 'pages/ucenter/index/index') {
                dom.getMyselfData();
              } else if (route == 'pages/pintuan/pintuan') {
                dom.getProductDetail();
                dom.getAssess();
                var assembleItemId = dom.data.assembleItemId;
                if (assembleItemId != null && assembleItemId != '') {
                  dom.getAssembleItemDetail();
                }
              }
            } else {
              console.log('未登录');
              console.log(res.data.content);
              dom.setData({
                hiddenName: false
              });
            }
          },
          fail: function (res) {
            console.log(res);
            console.log('未登录');
            dom.setData({
              hiddenName: false
            });
          }
        });
      }
    },
    fail: function (res) {
      console.log(res);
      console.log('未登录');
      dom.setData({
        hiddenName: false
      });
    }
  })
}

/**
 * 判断用户是否授权
 */
function checkLogin(e) {
  var route = e.route;
  var token = wx.getStorageSync('token');
  var openid = wx.getStorageSync('openid');
  var url = api.CheckTokenUrl;
  if (token != null && token != '') {
    wx.request({
      //后台接口地址
      url: api.CheckTokenUrl,
      data: {
        token: token
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        if (res.data.type == 'success') {
          console.log('已授权');
          e.setData({
            hiddenName: true
          });
          if (route == 'pages/index3/index3') {
            e.getMyselfData();
            e.getIndexData();
          } else if (route == 'pages/catalog/catalog') {
            e.getCatalog();
          } else if (route == 'pages/cart/cart') {
            e.getCartList();
            e.setData({
              currentAmount: 0.00,
              checkedAllStatus: false
            });
          } else if (route == 'pages/ucenter/index/index') {
            e.getMyselfData();
          } else if (route == 'pages/pintuan/pintuan') {
            e.getProductDetail();
            e.getAssess();
            var assembleItemId = e.data.assembleItemId;
            if (assembleItemId != null && assembleItemId != '') {
              e.getAssembleItemDetail();
            }
          }
        } else {
          console.log(res.data.content);
          console.log('未授权');
          e.setData({
            hiddenName: false
          });
        }
      },
      fail: function (res) {
        console.log(res);
        console.log('未授权');
        e.setData({
          hiddenName: false
        });
      }
    });
  } else {
    console.log('未授权');
    e.setData({
      hiddenName: false
    });
  }
}


module.exports = {
  loginByWeixin,
  checkLogin,
};











